﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MINERP.Statistics.ShopBill
{
    public partial class FrmShopBillIndex : Form
    {
        //参数
        private BLL.Statistics.ShopBillIndexBLL bll = new MINERP.BLL.Statistics.ShopBillIndexBLL();

        private static FrmShopBillIndex _instance;
        public static FrmShopBillIndex GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmShopBillIndex();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }

        public FrmShopBillIndex()
        {
            InitializeComponent();
        }

        private void FrmShopBillIndex_Load(object sender, EventArgs e)
        {
            try
            {
                BindShop();
                dtStartTime.DateTime = DateTime.Now;
                dtEndTime.DateTime = DateTime.Now;
                cbPay.Text = "未付款";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //查询账单索引
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                btnOK.Enabled = false;
                FindBill();
                btnOK.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //----------------公共方法-----------------
        #region 绑定商户
        /// <summary>
        /// 绑定商户
        /// </summary>
        private void BindShop()
        {
            try
            {
                List<BLL.Statistics.ShopBillIndexBLL.ShopCL> shops = bll.GetShops();
                cbShop.DisplayMember = "ShopName";
                cbShop.ValueMember = "ShopId";
                cbShop.DataSource = shops;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        private void FindBill()
        {
            try
            {
                DateTime start = dtStartTime.DateTime;
                DateTime end = dtEndTime.DateTime;
                if (start >= end.AddDays(1))
                {
                    MessageBox.Show("时间范围不合要求！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                int shopId=Convert.ToInt32(cbShop.SelectedValue);
                string payType = cbPay.Text.Trim();
                int status = 0;
                switch (payType)
                { 
                    case "全部":
                        status = 0; break;
                    case "未付款":
                        status = (int)COM.ShopBillStatus.UnCheckOut; break;
                    case "已付款":
                        status = (int)COM.ShopBillStatus.CheckedOut; break;
                }
                DataSet ds = bll.GetBillByTimeSpan(start, end, shopId, status);
                ds.WriteXml(@"C:\WINDOWS\xmlData\shopBillIndex.xml",XmlWriteMode.WriteSchema);
                report.Preview= previewControl1;
                report.RegisterData(ds);
                report.Load(Application.StartupPath + "\\shopBillIndex.frx");
                report.Show();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
